﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SRF.Common;
using SRF.Admin;
using SRF.Admin.Models;
using SRF.UI.Html;

namespace SRF.Web.Areas.Admin.Controllers
{
    [SRF.Security.Authorize]
    [SRF.Common.Logging.Log]
    public class DepartmentController : Controller
    {
        static SRF.Common.Logging.ILog logger = SRF.Common.Logging.LogManager.GetLogger("DepartmentController");

        DepartmentService departmentService = ObjectHelper.GetObject<DepartmentService>();
        //
        // GET: /Admin/Department/  
        [SRF.UI.ViewTitle("部门列表")]
        public ActionResult List()
        {
            SRF.UI.DataSource datasource = new UI.DataSource("admin.department.list", "admin.department.count");
            return View(datasource);
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns> 
        public ActionResult Edit(string id)
        {
            var deptList = departmentService.List();
            if (string.IsNullOrEmpty(id))
            {
                ViewData["title"] = "新增部门";
                ViewBag.DepartmentList = deptList;
                return View();
            }
            else
            {
                ViewData["title"] = "编辑部门";
                ViewBag.DepartmentList = deptList;
                var department = departmentService.Get(id);

                return View(department);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Department department)
        {
            ViewBag.DepartmentList = departmentService.List();
            var id = departmentService.Save(department);
            if (Request.IsAjaxRequest())
            {
                return Content(id);
            }
            else
            {
                return RedirectToAction("DepartmentEdit", "Admin", new { id = id });
            }
        }

        /// <summary>
        /// 查看部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns> 
        public ActionResult View(string id)
        {
            ViewBag.DepartmentList = departmentService.List();
            if (!string.IsNullOrEmpty(id))
            {
                var department = departmentService.Get(id);

                return View(department);
            }
            else
                return View();
        }

        public ActionResult Delete(string id)
        {
            if (departmentService.DeleteCheck(id))
            {
                departmentService.Delete(id);
                return new EmptyResult();
            }
            else
            {
                return Content("部门还有子部门或用户，不能删除");
            }
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            logger.Error(filterContext.RouteData.Values["action"], filterContext.Exception);

            base.OnException(filterContext);
        }
    }
}